package states;

/**
 *  A default class implementing IEvent.  All it does is take care of
 * the listener and toString. It is really useful if you want to use it
 * directly (without overriding anything)
 */
public class Event implements IEvent {

    private String name;
    private StateEngine listener;

    /**
     * Constructor to force naming of each event.
     * @param eventName The name used for logging
     */
    public Event(String eventName) {
        name = eventName;
    }

    /**
     * This causes the StateEngine to transition to the next state if
     * possible.  If there is no transaction for the current state + this
     * event, it will throw an IllegalStateException.
     */
    public void fireStateEvent() {
        listener.fireStateEvent(this);
    }

    /**
     * The StateEngine will set itself as listener for events from this
     * object.  I don't think it will ever need more than one listener,
     * hence the set instead of add; this should make it easier for
     * a developer to implement the interface (which will be useful if
     * you are also extending an event helper class)
     * 
     * @param engine the state engine that will get events 
     */
    public void setStateEventListener(StateEngine engine) {
        listener = engine;
    }

    /**
     * Used for logging purposes
     * @return the name set in the constructor.
     */
    public String toString() {
        return name;
    }
}
